﻿//////
////////#include"zmh.h"
//////
////////void test1()
////////{
////////	ZMH gilr;
////////	gilr.operator++(); // 前置
////////	gilr.operator++(0); // 后置
////////}
//////
////////void test2()
////////{
////////	Date d1(2004, 6, 24);
////////	Date d2(2024, 7, 14);
////////	cout << d1 << d2;
////////	d1 -= -30000;
////////	cout << d1;
////////	cout << d2 - -30000;
////////	cout << (d1 != d2) << endl << (d1 <= d2) << endl;
////////	cout << (d1 - d2) << "天" << endl;
////////	cout << d1-- << d1 << endl;
////////	cout << --d2 << d2 << endl;
////////	cout << d1++ << d1 << endl;
////////	cout << ++d2 << d2 << endl;
////////}
////////#include<stdlib.h>
////////class  A
////////{
////////public:
////////	A(int n,int b)
////////		:date((int*)malloc(16))
////////		, size(3)
////////		, capacity(3)
////////	{
////////		cout << size << endl;
////////	}
////////	A(int n = 4)
////////		:date((int*)malloc(13))
////////		, size(n)
////////		, capacity(n)
////////	{
////////		cout << size << endl;
////////	}
////////
////////	~A()
////////	{
////////		free(date);
////////		size = capacity = 0;
////////	}
////////
////////private:
////////	int* date;
////////	int size;
////////	int capacity;
////////
////////};
////////
////////void test3()
////////{
////////	A num(1);
////////	return ;
////////}
////////class A
////////{
////////public:
////////	A(int a = 4) 
////////		: _a(a)
////////		, _b((double)a) 
////////	{	
////////		cout << "A(int a = 4) " << endl;
////////	}
////////	~A() 
////////	{
////////		cout << "~A() " << endl;
////////	}
////////private:
////////	int _a;
////////	double _b;
////////};
////////#include <iostream>
////////using namespace std;
////////
////////class Date
////////{
////////public:
////////	Date(int year = 2020, int month = 1, int day = 1)
////////		:_year(year)
////////		, _month(month)
////////		, _day(day) {
////////		;
////////	}
////////	Date(const Date& d1)
////////		: _year(d1._year)
////////		, _month(d1._month)
////////		, _day(d1._day) {
////////		;
////////	}
////////	Date& operator+(int day)
////////	{
////////		_day += day;
////////		while (_day > GetMonthDay(_year, _month))
////////		{
////////			_day -= GetMonthDay(_year, _month);
////////			++_month;
////////			if (_month > 12)
////////			{
////////				++_year;
////////				_month = 1;
////////			}
////////		}
////////		return *this;
////////	}
////////	int GetMonthDay(int year, int month)
////////	{
////////		static int Month[13] = { -1,31,28,31,30,31,30,31,31,30,31,30,31 };
////////
////////		return month == 2
////////			&& ((!(year % 4) && (year % 100)) || !(year % 400))
////////			? Month[month] + 1 : Month[month];
////////	}
////////private:
////////	int _year;
////////	int _month;
////////	int _day;
////////};
////////
////////int main()
////////{
////////	return 0;
////////}
//////class A
//////{
//////	int* a;
//////public:
//////	A(int n = 10)
//////		:a(new int[n]) {}
//////};
//////class ZMH
//////{
//////public:
//////	//构造函数
//////	ZMH(int a = 13,int b = 14)
//////		:_a(a)
//////		,_b(b){}
//////	// _c
//////	//~ZMH(int a = 3){}
//////	//ZMH(const ZMH& tmp, int a) {};
//////	//ZMH& operator=(const ZMH& tmp) {};
//////	//ZMH* operator&() {};
//////	//const ZMH* operator&() const {};
//////	//ZMH& operator++(); 
//////	//ZMH operator++(int); 
//////private:
//////	int _a;
//////	int _b;
//////	A _c;
//////};
//////int main()
//////{
//////	ZMH z1;
//////	ZMH z2(z1);
//////	return 0;
//////}
//////#include<iostream>
//////using namespace std;
//////
//////class A
//////{
//////public:
//////	int c : 16; //4 5 6 7 8
//////	short b:8;
//////	char a : 4;
//////};
//////int main()
//////{
//////	struct A a ;
//////	a.a = 0xf;
//////	a.b = 0xff;
//////	a.c = 0xfffffff;
//////	return 0;
//////}
//////class myclassName
//////{
//////public:
//////	int a;
//////	int b;
//////	//类的方法
//////	//类的属性
//////}a;
//////int main()
//////{
//////	a = { 3,4 };
//////	return 0;
//////}
//////
//////class Date
//////{
//////public:
//////	void Init() 
//////	{}
//////private:
//////};
//////int main() { cout << sizeof(Date); }
//////
//////
//////class ZMH
//////{
//////public:
//////	static void Print();
//////private:
//////	static int _test;
//////	int _a;
//////	int _b;
//////};
//////
//////// 成员变量的定义
//////int ZMH::_test = 1314;
//////
//////// 成员函数的定义
//////void ZMH::Print()
//////{
//////	cout << ZMH::_test;
//////}
//////
//////int main()
//////{
//////	ZMH::Print();
//////	return 0;
//////}
//////class Date
//////{
//////public: // 公开->外面可使用这些数据
//////
//////	void Init(int year, int month, int Day);
//////
//////	void Init() // 默认是内联函数
//////	{
//////		_year = 2024;
//////		_month = 7;
//////		_day = 20;
//////	}
//////	void Print()
//////	{
//////		cout << _year << "-" << _month << "-" << _day << endl;
//////	}
//////private: // 私有
//////	int _year;
//////	int _month;
//////	int _day;
//////};
//////void Date::Init(int year, int month, int Day)
//////{
//////	_year = year;
//////	_month = month;
//////	_day = Day;
//////}
//////
//////int main()
//////{
//////	Date d1, d2;
//////	d1.Init();
//////	d1.Print();
//////
//////	d2.Init(2012, 12, 4);
//////	d2.Print();
//////	return 0;
//////}
////
////int main()
////{
////	return 0;
////}
////
//////
//////
//
//////
//////void SeqlistPrint(SL* p)
//////{
//////	for (int i = 0; i < p->size; ++i)
//////	{
//////		printf("%d ", p->DateElement[i]);
//////	}
//////	printf("\n");
//////}
//////void SeqlistDestory(SL* p)
//////{
//////	free(p->DateElement);
//////	p->DateElement = NULL;//因为free函数只会释放掉p->DateElement所指向的空间，但该指针仍指向那片空间，
//////	p->size = 0;//为了代码的健壮性，我们应的将其置成NULL，以及其余参数置成0；
//////	p->capacity = 0;
//////}
//////void SeqlistPushBack(SL* p, Datype x)
//////{
//////	SeqlistChack(p);
//////
//////	//p->DateElement[p->size] = x;
//////	//p->size++;
//////	// //因为尾插与头插可以复用 任意插入 这一接口；所有对其进调整
//////
//////	SeqlistInsert(p, x, p->size + 1);
//////	//这里的第三个参数的具体含义得把握清楚，是你要插入元素到第几个位置，p->size是指当前已存元素个数
//////	//所以尾插就要，p->size+1
//////}
//////void SeqlistPushFront(SL* p, Datype x)
//////{
//////	SeqlistChack(p);
//////
//////	//int end = p->size - 1;
//////	//while (end>=0)
//////	//{
//////	//	p->DateElement[end + 1] = p->DateElement[end];
//////	//	end--;
//////	//}
//////	//p->DateElement[0] = x;
//////	//p->size++*/;
//////	//因为尾插与头插可以复用 任意插入 这一接口；所有对其进调整
//////	SeqlistInsert(p, x, 1);
//////}
//////void SeqlistInsert(SL* p, Datype x, int k)
//////{
//////}
//////void SeqlistPopBack(SL* p)
//////{
//////
//////	SeqlistRemove(p, p->size);
//////
//////}
//////void SeqlistPopFront(SL* p)
//////{
//////
//////	SeqlistRemove(p, 1);
//////
//////}
//////void SeqlistRemove(SL* p, int k)
//////{
//////	assert(p != NULL);
//////	assert(k >= 1);
//////	assert(k <= p->size);
//////	int start = k - 1;
//////	while (start <= p->size - 1)
//////	{
//////		p->DateElement[start] = p->DateElement[start + 1];
//////		start++;
//////	}
//////	p->size--;
//////}
//////int SeqlistIndex(SL* p, int x)
//////{
//////	//在一个数组中，查找K元素，返回该下标；
//////	int i = 0;
//////	for (i = 0; i < p->size; ++i)
//////	{
//////		if (p->DateElement[i] == x)
//////		{
//////			return i;
//////		}
//////	}
//////	return -1;

#include<iostream>
#include<assert.h>
using namespace std;

template<class T>
class SeqList
{
public:
	SeqList(int n = 4)
		:_val(new T[n])
		, _size(0)
		, _capacity(n) {}
	~SeqList()
	{
		delete[] _val;
		_size = _capacity = 0;
	}
	void Chack()
	{
		if (_size >= _capacity)
		{
			T* tmp = new T[2 * _capacity];
			memcpy(tmp, _val, sizeof(T) * _capacity);
			delete[] _val;
			_val = tmp;
			_capacity *= 2;
		}
	}
	void Print()
	{
		for (int i = 0; i < _size; ++i)
			cout << _val[i] << " ";
		cout << endl;
	}
	void Insert(T x, int k);
	void Remove(int k);
	int  Find(T x);
	void Revise(T x, int k);
private:
	T* _val;
	int _size;
	int _capacity;
};

template<class T>
void SeqList<T>::Insert(T x, int k)
{
	Chack();
	assert(k >= 1 && k <= _size + 1);

	int end = _size - 1;
	while (end >= k - 1)
	{
		_val[end + 1] = _val[end];
		end--;
	}

	_val[k - 1] = x;
	++_size;
}

template<class T>
void SeqList<T>::Remove(int k)
{
	assert(k >= 1 && k <= _size);

	int start = k - 1;
	while (start + 1 < _size)
	{
		_val[start] = _val[start + 1];
		start++;
	}

	--_size;
}

template<class T>
int SeqList<T>::Find(T x)
{
	for (int i = 0; i < _size; ++i)
	{
		if (_val[i] == x)
		{
			return i;
		}
	}
	return -1;
}

template<class T>
void SeqList<T>::Revise(T x, int k)
{
	assert(k >= 1 && k <= _size);
	_val[k - 1] = x;
}

int main()
{
	SeqList<int> s1(4);
	s1.Insert(1, 1);
	s1.Insert(2, 1);
	s1.Insert(3, 1);
	s1.Insert(4, 1);
	s1.Insert(5, 1);
	s1.Print();
	cout << s1.Find(1) << endl;
	s1.Remove(3);
	s1.Print();
	s1.Revise(1314, 3);
	s1.Print();
	return 0;
}